layui.define(['table', 'form', 'admin', 'upload'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    table.render({
        elem: "#LAY-livecategory-manage",
        url: "/admin/live/getcategorys",
        cols: [
            [
                { field: "id", width: 100, title: "ID", sort: true },
                { field: "sort", width: 100, title: "排序", sort: true },
                {
                    field: "",
                    title: "默认图标",
                    width: 100,
                    templet: function (d) {
                        return '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' + d.icon + '">';
                    },
                },
                { field: "title", title: "名称", width: 200 },
                {
                    field: "", title: "所属分类", minWidth: 200, templet: function (d) {
                        return d.category_top ? d.category_top.title : '';
                    },
                },
                {
                    title: "操作",
                    width: 180,
                    align: "center",
                    fixed: "right",
                    toolbar: "#table-livecategorylist-livecategory",
                },
            ],
        ],
        page: false,
        height: "full-140",
        text: "对不起，加载出现异常！",
        even: true,
    });

    // 顶级分类
    table.render({
        elem: "#LAY-livecategorytop-manage",
        url: "/admin/live/getcategorytop",
        cols: [
            [
                { field: "id", width: 100, title: "ID", sort: true },
                { field: "sort", width: 100, title: "排序", sort: true },
                {
                    field: "",
                    title: "默认图标",
                    width: 150,
                    templet: function (d) {
                        return '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' + d.icon + '">';
                    },
                },
                {
                    field: "",
                    title: "选中图标",
                    width: 150,
                    templet: function (d) {
                        return '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' + d.second_icon + '">';
                    },
                },
                { field: "title", title: "分类名称" },
                {
                    title: "操作",
                    width: 150,
                    align: "center",
                    fixed: "right",
                    toolbar: "#table-livecategorytoplist",
                },
            ],
        ],
        page: false,
        height: "full-140",
        text: "对不起，加载出现异常！",
        even: true,
    });


    //监听工具条-二级分类
    table.on("tool(LAY-livecategory-manage)", function (obj) {
        if (obj.event === "hide") {
            admin.req({
                url: "/admin/live/category_hide_post",
                data: { id: obj.data.id },
                success(res) {
                    if (res.code == 0) {
                        table.reload("LAY-livecategory-manage");
                        layer.msg(res.msg, { icon: 1 });
                    } else {
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        } else if (obj.event === "show") {
            admin.req({
                url: "/admin/live/category_show_post",
                data: { id: obj.data.id },
                success(res) {
                    if (res.code == 0) {
                        table.reload("LAY-livecategory-manage");
                        layer.msg(res.msg, { icon: 1 });
                    } else {
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        } else if (obj.event === "edit") {
            layer.open({
                type: 2,
                title: "编辑",
                content: "/admin/live/category_edit?id=" + obj.data.id,
                maxmin: true,
                area: ["650px", "380px"],
            });
        }
    });

    //监听工具条-顶级分类
    table.on("tool(LAY-livecategorytop-manage)", function (obj) {
        if (obj.event === "hide") {
            admin.req({
                url: "/admin/live/category_top_hide_post",
                data: { id: obj.data.id },
                success(res) {
                    if (res.code == 0) {
                        table.reload("LAY-livecategorytop-manage");
                        layer.msg(res.msg, { icon: 1 });
                    } else {
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        } else if (obj.event === "show") {
            admin.req({
                url: "/admin/live/category_top_show_post",
                data: { id: obj.data.id },
                success(res) {
                    if (res.code == 0) {
                        table.reload("LAY-livecategorytop-manage");
                        layer.msg(res.msg, { icon: 1 });
                    } else {
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        } else if (obj.event === "edit") {
            layer.open({
                type: 2,
                title: "编辑",
                content: "/admin/live/category_top_edit?id=" + obj.data.id,
                maxmin: true,
                area: ["650px", "380px"],
            });
        }
    });


    //上传图片
    var iconSrc = $("#LAY_iconImgSrc");
    upload.render({
        url: "/admin/index/noOperation",
        elem: "#LAY_iconImgUpload",
        auto: true,
        choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: "/admin/live/signForCos",
                success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime,
                                });
                            },
                            Domain:res.data.imgDomain,
                        });
                        cos.putObject(
                            {
                                Bucket: res.data.bucket,
                                Region: res.data.region,
                                Key: "images/" + res.data.filename + choosefile.name,
                                Body: choosefile,
                            },
                            function (err, data) {
                                layer.closeAll("loading");
                                if (err) {
                                    console.log("上传失败", err);
                                    layer.msg("上传失败", { icon: 5 });
                                } else {
                                    if (data.statusCode == 200) {
                                        iconSrc.val("https://" + data.Location);
                                    } else {
                                        layer.msg("图片上传失败", { icon: 5 });
                                    }
                                }
                            }
                        );
                    } else {
                        layer.closeAll("loading");
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        },
    });

    var iconSrc2 = $("#LAY_iconImgSrc2");
    upload.render({
        url: "/admin/index/noOperation",
        elem: "#LAY_iconImgUpload2",
        auto: true,
        choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: "/admin/live/signForCos",
                success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime,
                                });
                            },
                            Domain:res.data.imgDomain,
                        });
                        cos.putObject(
                            {
                                Bucket: res.data.bucket,
                                Region: res.data.region,
                                Key: "images/" + res.data.filename + choosefile.name,
                                Body: choosefile,
                            },
                            function (err, data) {
                                layer.closeAll("loading");
                                if (err) {
                                    console.log("上传失败", err);
                                    layer.msg("上传失败", { icon: 5 });
                                } else {
                                    if (data.statusCode == 200) {
                                        iconSrc2.val("https://" + data.Location);
                                    } else {
                                        layer.msg("图片上传失败", { icon: 5 });
                                    }
                                }
                            }
                        );
                    } else {
                        layer.closeAll("loading");
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        },
    });

    //查看照片
    admin.events.coverImgPreview = function (othis) {
        var pic_obj_id = othis.data('idname');
        var src = $('#' + pic_obj_id).val();
        layer.photos({
            photos: {
                "title": "查看图标" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    //列表查看照片
    admin.events.photosPreview = function (othis) {
        let urls = othis.attr("src");
        let urlarr = new Array();
        $.each(urls.split(","), function (i, val) {
            urlarr.push({ src: val });
        });
        layer.photos({
            photos: {
                title: "查看照片", //相册标题
                data: urlarr,
            },
            shade: 0.01,
            closeBtn: 1,
            anim: 5,
        });
    };

    exports('category', {})
});